package com.aidansmeaton.weightmanager.util;


/**
 * This class provides utility functions for formatting
 * time strings, such as ISO-8601 format into a
 * "human readable" format, and vice versa.
 * 
 * @author Aidan Smeaton
 */
public class CalorieHelper {	

	private static final double KG_IN_POUNDS = 0.4536F;
	private static final int POUNDS_IN_STONE = 14;
	
	public static int getCaloriesFromGrade(int grade){
		switch (grade){
		case 1:	return 25;
		case 2: return 50;
		case 3: return 100;
		case 4: return 150;
		case 5: return 50;
		case 6: return 100;
		case 7: return 200;
		case 8: return 300;
		case 9: return 75;
		case 10: return 150;
		case 11: return 300;
		case 12: return 450;
		default: return 0;
		}
	}
	
	public static int calculateRecommendedCaloriesPlan(int age, int weight, boolean isFemale, int exercise, boolean imperial) {
		if (!imperial) weight = ((int)(weight * KG_IN_POUNDS));
		int bmr = (int) calculateBMR(age, isFemale, weight);
		int req = (int) (bmr * calculatePAL(exercise));
		return Math.max((((req - 600) / 100) * 100), 1400);
	}
	
	private static double calculateBMR(int age, boolean isFemale, int weight) {

		double kg = KG_IN_POUNDS * weight;

		// hard-coded BMR values based on age, gender, and weight
		if (age >= 18 && age < 30) {
			return (isFemale) ? (kg * 14.7) + 496 : (kg * 15.3) + 679;
		} else if (age >= 30 && age < 60) {
			return (isFemale) ? (kg * 8.7) + 829 : (kg * 11.6) + 879;
		} else if (age >= 60) {
			return (isFemale) ? (kg * 10.5) + 596 : (kg * 13.5) + 487;
		} else
			return -1;
	}

	private static double calculatePAL(int activityLevel) {
		switch (activityLevel) {
		case 0:
			return 1.3; // hard-coded PAL value
		case 1:
			return 1.55; // hard-coded PAL value
		case 2:
			return 1.78; // hard-coded PAL value
		case 3:
			return 2.1; // hard-coded PAL value
		}
		return 0;
	}
	
	public static String weightToString(int weight, boolean imperial) {
		if (!imperial){
			switch (weight){
			case 0:		return "nothing yet";
			default:	return weight + " kg";
			}
		}
		String s = "";
		String p = "";
		switch (weight/POUNDS_IN_STONE){
		case 0:														break;
		case 1:		s += "1 stone";									break;
		default:	s += (weight/POUNDS_IN_STONE) + " stones";		break;
		}
		switch (weight%POUNDS_IN_STONE){
		case 0:														break;
		case 1:		p += "1 pound";									break;
		default:	p += (weight%POUNDS_IN_STONE) + " pounds";		break;
		}
		if (s.length() == 0){
			if (p.length() == 0) return "nothing yet";
			else return p;
		}
		else if (p.length() == 0) return s;
		else return s + " " + p;
	}
	
}
